import { defineStore } from "pinia";
export const useOrderStore = defineStore("order", {
  state: () => ({
    orders: [],
    currentTab: "all",
    loading: false,
    error: null,
  }),
  getters: {
    filteredOrders: (state) => {
      if (state.currentTab === "all") return state.orders;
      return state.orders.filter((order) => order.status === state.currentTab);
    },
    tabStats: (state) => {
      const stats = {
        all: 0,
        making: 0,
        completed: 0,
      };
      state.orders.forEach((order) => {
        if(order) {
            stats.all++, 
            stats[order.status] = (stats[order.status] || 0) + 1;
        }
        
      });
      return stats;
    },
  },
  actions: {
    async fetchOrders() {
      try {
        this.loading = true;
        // TODO
        const orderList = [
          {
            id: 1,
            userId: 101,
            createdAt: "2023-05-15T10:30:00Z",
            updatedAt: "2023-05-15T10:30:00Z",
            status: "all", // 1 待制作 2 已完成 3 已取消
            remark: "要多多加辣，谢谢！",
            orderItems: [
              {
                id: 1001,
                productId: 2001,
                quantity: 2, // 数量
                createdAt: "2023-05-15T10:30:00Z",
                updatedAt: "2023-05-15T10:30:00Z",
                product: {
                  id: 2001,
                  name: "毛血旺",
                  image: "https://example.com/image.jpg",
                  recommendNumber: 100, // 推荐指数
                },
                specifications: [
                  {
                    id: 5001,
                    specificationId: 3001,
                    optionId: 4001,
                    createdAt: "2023-05-15T10:30:00Z",
                    updatedAt: "2023-05-15T10:30:00Z",
                    specification: {
                      id: 3001,
                      name: "辣度",
                    },
                    option: [
                      {
                        id: 4001,
                        value: "中辣",
                      },
                      {
                        id: 4002,
                        value: "重辣",
                      },
                    ],
                  },
                  {
                    id: 5002,
                    specificationId: 3002,
                    optionId: 4003,
                    createdAt: "2023-05-15T10:30:00Z",
                    updatedAt: "2023-05-15T10:30:00Z",
                    specification: {
                      id: 3002,
                      name: "是否加香菜",
                    },
                    option: [
                      {
                        id: 4003,
                        value: "加香菜",
                      },
                    ],
                  },
                ],
              },
              {
                id: 1002,
                productId: 2002,
                quantity: 1,
                createdAt: "2023-05-15T10:30:00Z",
                updatedAt: "2023-05-15T10:30:00Z",
                product: {
                  id: 2002,
                  name: "番茄炒蛋",
                  image: "https://example.com/image.jpg",
                  recommendNumber: 100, // 推荐指数
                },
                specifications: [], // 没有规格
              },
            ],
            user: {
              id: 101,
              name: "张三",
            },
          },
          {
            id: 2,
            userId: 102,
            createdAt: "2023-05-16T14:15:22Z",
            updatedAt: "2023-05-16T14:20:10Z",
            status: "all", // 1 待制作 2 已完成 3 已取消
            remark: null,
            orderItems: [
              {
                id: 1003,
                productId: 2003,
                quantity: 3,
                createdAt: "2023-05-16T14:15:22Z",
                updatedAt: "2023-05-16T14:15:22Z",
                product: {
                  id: 2003,
                  name: "肉饼汤",
                  image: "https://example.com/image.jpg",
                  recommendNumber: 100, // 推荐指数
                },
                specifications: [],
              },
            ],
            user: {
              id: 102,
              name: "李四",
            },
          },
        ];
        this.orders = orderList;
      } catch (error) {
        this.error = error.message;
        this.orders = [];
      } finally {
        this.loading = false;
      }
    },
    cancelOrder(orderId) {
      const index = this.orders.findIndex((o) => o.id === orderId);
      if (index !== -1) {
        this.orders[index].status = "canceled";
      }
    },
    setCurrentTab(tab) {
      this.currentTab = tab;
    },
  },
});
